import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Banner, HotTag, SongSheet } from '@app/models';
import { BANNER, HOTTAG, PERSONALIZED } from '@assets/constance';
import { map, Observable } from 'rxjs';
import { API_CONFIG, ServicesModule } from './services.module';

@Injectable({
  providedIn: ServicesModule,
})
export class HomeService {
  constructor(
    private http: HttpClient,
    @Inject(API_CONFIG) private baseUrl: string
  ) {}

  getBanners(): Observable<Banner[]> {
    return this.http
      .get(`${this.baseUrl}/${BANNER}`)
      .pipe(map((res: any) => res.banners));
  }

  getHotTags(): Observable<HotTag[]> {
    return this.http.get(`${this.baseUrl}/${HOTTAG}`).pipe(
      map((res: any) => {
        return res.tags
          .sort((x: HotTag, y: HotTag) => {
            return x.position - y.position;
          })
          .slice(0, 5);
      })
    );
  }

  getPersonalSheetList(limit: number = 30): Observable<SongSheet[]> {
    return this.http
      .get(`${this.baseUrl}/${PERSONALIZED}`, { params: { limit } })
      .pipe(map((res: any) => res.result.slice(0, 16)));
  }
}
